#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author : hai.long
# Comment : 数据库相关配置
import pymysql


class MysqlDB(object):
    conn = None
    cursor = None

    def __init__(self, host='127.0.0.1', user="root", passwd="longhai123", db=""):
        self.conn = pymysql.connect(host=host, user=user, passwd=passwd, database=db)
        self.cursor = self.conn.cursor()

    def insert(self, insert_sql):
        try:
            self.conn.ping(True)
            self.cursor.execute(insert_sql)
            insert_id = self.conn.insert_id()
            self.conn.commit()
            return insert_id
        except Exception as e:
            raise e

    def delete(self, delete_sql):
        try:
            self.conn.ping(True)
            self.cursor.execute(delete_sql)
            self.conn.commit()
            return True
        except Exception as e:
            raise e

    def update(self, update_sql):
        try:
            self.conn.ping(True)
            self.cursor.execute(update_sql)
            self.conn.commit()
            return True
        except Exception as e:
            raise e

    def query_all(self, query_sql):
        try:
            self.conn.ping(True)
            self.cursor.execute(query_sql)
            query_set = list(self.cursor.fetchall())
            return query_set
        except Exception as e:
            raise e

    def query_one(self, query_sql):
        try:
            self.conn.ping(True)
            self.cursor.execute(query_sql)
            query_set = self.cursor.fetchone()
            return query_set
        except Exception as e:
            raise e

    def execute(self, sql):
        try:
            self.conn.ping(True)
            self.cursor.execute(sql)
            self.conn.commit()
            return True
        except Exception as e:
            raise e


# host=env_data["host"], user=env_data["user"], passwd=env_data["password"], db="test_platform"
mysql_db = MysqlDB(host='124.220.103.163', user=env_data["user"], passwd=env_data["password"], db="test_platform")

if __name__ == '__main__':

    pass
